layui.use(['table', 'admin','laydate', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
        elem: '#startTime'
        ,value:new Date()
    });

    laydate.render({
        elem: '#endTime'
        ,value:new Date()
    });


    /**
     * 临时车超额审批标准管理
     */
    var FiTempCarAppStandExa = {
        tableId: "fiTempCarAppStandExaTable"
    };

    /**
     * 初始化表格的列
     */
    FiTempCarAppStandExa.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'operateName', title: '项目', style:'font-weight: 600;'},
            {field: 'trainNumber', title: '车次', style:'font-weight: 600;', sort: true},
            {field: 'startDate', title: '发车日期', style:'font-weight: 600;', sort: true},
            {field: 'fayunTimes', title: '发运时间', sort: true},
            {field: 'dispatchNumber', title: '运输单号', style:'font-weight: 600;',templet: '#dispatchNumberTpl'},
            {field: 'carnumber', title: '车牌号', style:'font-weight: 600;', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'driver1', title: '司机', style:'font-weight: 600;', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'tel1', title: '联系方式', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'privatecarType', title: '车型', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'mileageTraveled', title: '行驶里程', style:'background-color: #dbf1e1;font-weight: 600;', sort: true},
            {field: 'loadingType', title: '装车方', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'loadingPersonnel', title: '装车人员', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'privatecarSurevolume', title: '准载方数m3', style:'background-color: #dbf1e1;font-weight: 600;', sort: true},
            {field: 'toovolume', title: '总方数m3', style:'font-weight: 600;', totalRow: true, sort: true},
            {field: 'carrierName', sort: true, title: '承运商', style:'font-weight: 600;'},
            {field: 'lineName', sort: true, title: '线路', style:'font-weight: 600;'},
            {field: 'loadingMoney', sort: true, title: '装车费￥', style:'font-weight: 600;', totalRow: true},
            {field: 'xiechefei', sort: true, title: '卸车费￥', style:'font-weight: 600;', totalRow: true},
            {field: 'matchRs', title: '结算状态', style:'font-weight: 600;'},
            {field: 'settlementMonth', title: '结算月份', style:'font-weight: 600;'},
            {field: 'freightMoney', title: '运费￥', style:'font-weight: 600;',totalRow: true},
            {field: 'bzPrice', title: '标准单价', style:'font-weight: 600;'},
            {field: 'bzFreightMoney', title: '标准运费', style:'font-weight: 600;'},
            {field: 'bzRate', title: '限定超额比%', style:'font-weight: 600;'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FiTempCarAppStandExa.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId").val();
        queryData['condition'] = $("#condition").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['matchRs'] = $("#matchRs").val();
        queryData['settlementMonth'] = $("#settlementMonth").val();
        table.reload(FiTempCarAppStandExa.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };


    /**
     * 导出excel按钮
     */
    FiTempCarAppStandExa.exportExcel = function () {
        var checkRows = table.checkStatus(FiTempCarAppStandExa.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    FiTempCarAppStandExa.btnShenhe = function () {
        var checkRows = table.checkStatus(FiTempCarAppStandExa.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要审核的数据");
        } else {
            var ids = checkRows.data.map(function(item) {
                return item.id;
            });
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/fiTempCarAppStandExa/shenhe", function (data) {
                    if(data.success){
                        Feng.success("操作成功!");
                        table.reload(FiTempCarAppStandExa.tableId);
                    }else{
                        Feng.error("操作失败!" + data.message + "!");
                    }
                }, function (data) {
                    Feng.error("操作失败!" + data.responseJSON.message + "!");
                });
                ajax.set("ids", ids.join(","));
                ajax.start();
            };
            Feng.confirm("是否对选择的记录发起审核?", operation);
        }
    };
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FiTempCarAppStandExa.tableId,
        url: Feng.ctxPath + '/fiTempCarAppStandExa/listPage',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: FiTempCarAppStandExa.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        FiTempCarAppStandExa.search();
    });

    // 审核
    $('#btnShenhe').click(function () {
        FiTempCarAppStandExa.btnShenhe();
    });

    // 工具条点击事件
    table.on('tool(' + FiTempCarAppStandExa.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'tooDetailed') {
            layer.open({
                type: 2,
                title: '配送派车信息',
                area: ['1250px', '570px'],
                anim: 5,
                content: Feng.ctxPath + '/tDispatch/tooDetailed?dispatchNumber=' + data.dispatchNumber,
            });
        }
    });
});
